package com.erdos.common.util.log;

/**
 * Utilites for get the caller method name and class name
 */
public class StackContextUtil 
{
    /**
     * get the caller method  name
     */ 
    static public String getCallerMethodName(int backLevel)
    {
        String callerMethodName = "";
        
        try 
        {
            Throwable ts = new Throwable();
        
            StackTraceElement[] stackElement = ts.getStackTrace();
        
             callerMethodName =  stackElement[backLevel + 1].getMethodName();
        
        }
        catch (Exception ex)
        {
            callerMethodName = "";
        }        
        
        return callerMethodName;
    }


    /**
     * get the caller class  name
     */ 
    static public String getCallerClassName(int backLevel)
    {
        String callerClassName = "";
        try 
        {
           Throwable ts = new Throwable();
        
           StackTraceElement[] stackElement = ts.getStackTrace();
           
           callerClassName = stackElement[backLevel + 1].getClassName();
        }
        catch (Exception ex)
        {
            callerClassName = "";
        }
        return callerClassName;
 
    }
    
    /**
     * get the caller class  name
     */ 
    static public int getCallerLineNumber(int backLevel)
    {
        int callerLineNumber = -1;
        try 
        {
           Throwable ts = new Throwable();
        
           StackTraceElement[] stackElement = ts.getStackTrace();
           
           callerLineNumber = stackElement[backLevel + 1].getLineNumber();
        }
        catch (Exception ex)
        {
        	callerLineNumber = -1;
        }
        return callerLineNumber;
 
    }
} 